package tests;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import model.User;
import model.Template;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * This class tests the User templates functionality
 * 
 * @author Sukhjinder K. Narula
 * @since June 2013
 *
 * @version $Revision: 1.0 $
 */
public class TestUserTemplates {

	User user;
	HashMap<String, String> properties;
	List<Template> templates;
	Template template1, template2;
	HashMap<String, String> templatesMap;
	
	/**
	 * This method is called before every test case, it creates a new user, sets its properties and adds two templates to its
	 * list of templates.
	 */
	@Before
	public void setUp()
	{
		user=new User("sknarula");
		properties=new HashMap<String,String>();
		properties.put("Id", "1");
		properties.put("Name","Laura");
		
		template1 = new Template(1, "My Template 1", "This is my template 1 - ${name}");
		template1.setId(1);
		template2 = new Template(1, "My Template 2", "This is my template 2 - ${name}");
		template2.setId(2);
		templates = new ArrayList<Template>();
		templates.add(template1);
		templates.add(template2);
		
		user.setTemplates(templates);

		templatesMap = new HashMap<String,String>();
		for(Template template : templates){
			templatesMap.put(template.getTitle(), template.getBody());
		}
		
	}
	/**
	 * This method is called after every test case to tear down the user object and force garbage collection.
	 */
	
	@After
	public void tearDown()
	{
		user=null;
		System.gc();
	}
	
	/**
	 * This test case tests to see if the template exits return true if template exists
	 */
	@Test
	public void testTemplateExistsReturnsTrue() {
		assertTrue(user.templateExists(template1.getTitle()));
	}

	/**
	 * This test case tests to see if the template exits return false if template does not exists
	 */
	@Test
	public void testTemplateExistsReturnsFalse() {
		assertFalse(user.templateExists("test template"));
	}

	/**
	 * This test ensures that given an id, the get template method returns the correct object
	 */
	@Test
	public void testGetTemplateById() {
		Template result = user.getTemplate(1);
		assertEquals(result, template1);
	}
	
	/**
	 * This test ensures that given an key, the get template method returns the correct object
	 */
	@Test
	public void testGetTemplateByKey() {
		Template result = user.getTemplate("My Template 2");
		assertEquals(result, template2);
	}
	
	/**
	 * This test case tests the getTemplates method by ensuring that the size of the list of templates equals to the one specified.
	 */
	@Test
	public void testGetTemplatesBySize() {
		List<Template> result=user.getTemplates();
		assertEquals(2,result.size());
	}

	/**
	 * This test case tests the getTemplates method by ensuring that the list of the templates return is equals to whats setup
	 */
	@Test
	public void testGetTemplatesByData() {
		List<Template> result=user.getTemplates();
		assertEquals(result,templates);
	}

	/**
	 * This test case tests the getTemplatesHashMap generate the same hashmap of templates created as a part of setup
	 */
	@Test
	public void getTemplateHashMap() {
		HashMap<String, String> result=user.getTemplateHashMap();
		assertEquals(result,templatesMap);
	}
	
	/**
	 * This test ensures that transfer object for template is created appropriately
	 */
	@Test
	public void testGetTransferObjectToGetTemplateHashMap() {
		@SuppressWarnings("serial")
		HashMap<String, String> templateTransferObject = new HashMap<String, String>(){{
			put("Id", String.valueOf(2));
			put("UserId", String.valueOf(1));
			put("Title", "My Template 2");
			put("Body",  "This is my template 2 - ${name}");
		}};
		
		assertEquals(templateTransferObject, template2.getTransferObject());		
	}
	
	/**
	 * This test ensures that transfer object is created appropriately
	 */
	@Test
	public void testGetTransferObjectToGetEmptyHashMap() {
		HashMap<String, String> meetingTransferObject = new HashMap<String, String>();
		assertFalse(meetingTransferObject.equals(template1.getTransferObject()));
	}
	
	/**
	 * This test case tests the getUserId method
	 */
	@Test
	public void testGetUserId() {
	
		assertEquals(1,template1.getUserId());
	}
	
	/**
	 * This test case tests the setUserId method
	 */
	@Test
	public void testSetUserId() {
	
		template1.setUserId(2);
		assertEquals(2,template1.getUserId());
	}
	
	/**
	 * This test case tests the setTitle method
	 */
	@Test
	public void testSetTitle() {
	template1.setTitle("new title");
		assertEquals("new title",template1.getTitle());
	}
	
	/**
	 * This test case tests the setBody method
	 */
	@Test
	public void testSetBody() {
	template1.setBody("new body");
		assertEquals("new body",template1.getBody());
	}
	
	/**
	 * This test case tests the getTransferObject method
	 */
	@Test
	public void testGetTeansferObject() {
		HashMap<String, String> result = new HashMap<String, String>();
		result=template1.getTransferObject();
		assertEquals(4,template1.getTransferObject().size());
		assertNotNull(result);
	}
	

}
